import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.junit.Test;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;


public class testZip {

    @Test
    public void demo() throws Exception{
        List<String> l=new ArrayList<>();
        l.add("20a42e962dad4afeac169c2fda016923");
        l.add("7cde7696d9104c7b840416aa7e958aa7");

        List<String> l1=new ArrayList<>();
        l1.add("0527e7779e46456baec54b9d5b49edb8");

        Map<String,List<String>> mp=new HashMap<>();
        mp.put("爱奇艺",l);
        mp.put("百度",l1);
        FileOutputStream fou = new FileOutputStream("d:/工商文件.zip");
        ZipOutputStream zos = new ZipOutputStream(fou);
        for (Map.Entry<String, List<String>> entry : mp.entrySet()) {
            List<String> fileUUids = entry.getValue();
            int i=0;
            ZipOutputStream merchant = new ZipOutputStream(zos);
            zos.putNextEntry(new ZipEntry((entry.getKey()+".zip")));
            for (String fileUUid:fileUUids) {
                i++;
                merchant.putNextEntry(new ZipEntry(i+"a.jpg"));
                FileInputStream in=new FileInputStream("E:\\temp\\"+fileUUid);
                byte[] buf=new byte[1024*1024];
                int len;
                while((len=in.read(buf))>0){
                    merchant.write(buf,0,len);
                }
                merchant.closeEntry();
                in.close();
            }
            merchant.finish();
        }
        zos.finish();
        zos.close();

        fou.flush();
        fou.close();
    }


    @Test
    public void demo1() throws Exception{
        List<String> l=new ArrayList<>();
        l.add("20a42e962dad4afeac169c2fda016923");
        l.add("7cde7696d9104c7b840416aa7e958aa7");
        List<String> l1=new ArrayList<>();
        l1.add("0527e7779e46456baec54b9d5b49edb8");
        Map<String,List<String>> mp=new HashMap<>();
        mp.put("爱奇艺",l);
        mp.put("百度",l1);
        FileOutputStream fou = new FileOutputStream("d:/工商文件.zip");
        ZipArchiveOutputStream archOuts =  new ZipArchiveOutputStream(fou);
        archOuts.setEncoding("GBK");
        for (Map.Entry<String, List<String>> entry : mp.entrySet()) {
            List<String> fileUUids = entry.getValue();
            generateZip(entry.getKey(),archOuts,fileUUids);
        }
        archOuts.finish();
        archOuts.close();
        fou.close();
    }

    private void generateZip(String name,ZipArchiveOutputStream archOuts, List<String> fileUUids) throws Exception{
        archOuts.putArchiveEntry(new ZipArchiveEntry(name+".zip"));
        ZipArchiveOutputStream merchant = new ZipArchiveOutputStream(archOuts);
        merchant.setEncoding("GBK");
        int i=0;
        for (String fileUUid:fileUUids) {
            i++;
            ZipArchiveEntry zipEntry=new ZipArchiveEntry(new File("E:\\temp\\"+fileUUid),i+"股权存续.jpg");
            merchant.putArchiveEntry(zipEntry);
            FileInputStream in=new FileInputStream("E:\\temp\\"+fileUUid);
            byte[] buf=new byte[1024*1024];
            int len;
            while((len=in.read(buf))>0){
                merchant.write(buf,0,len);
            }
            merchant.closeArchiveEntry();
            merchant.flush();
            in.close();
        }
        merchant.finish();
        archOuts.closeArchiveEntry();
        archOuts.flush();
    }

}
